﻿package {

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	public class ImageObject {
		
		private var image:MovieClip;
		private var editor:Editor;
		private var filename:String;
		private var loader:ImageLoader
		
		private var offset:Point;
		private var mDown:Boolean;
		
		public function ImageObject(e:Editor) {
			editor = e;
			loader = new ImageLoader();
			image = loader.getImage();
			image.x = 100;
			image.y = 100;
			loader.addEventListener(Event.COMPLETE, imageLoaded);
			image.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
			image.addEventListener(MouseEvent.MOUSE_UP, mouseUpListener);
			image.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
		}
		
		public function loadImage() {
			loader.loadImage();
		}
		
		private function imageLoaded(e:Event) {
			editor.addChild(image);
			filename = loader.filename;
			editor.elements.push(this);
			editor.elementTypes.push("image");
			image.addEventListener(MouseEvent.MOUSE_UP, mouseUpListener);
		}
		
		private function mouseUpListener(e:MouseEvent):void {
			mDown = false;
			if (editor.cursor != "eraser") editor.showProperties("image", null, this);
			else {
				var index:int = editor.elements.indexOf(this);
				editor.elements.splice(index, 1);
				editor.elementTypes.splice(index, 1);				
				if (editor.contains(image)) editor.removeChild(image);
			}
			
		}
		
		public function setProp(prop:String, val:Number) {
			switch (prop) {
				case "x":
					image.x = val;
					break;
				case "y":
					image.y = val;
					break;
				case "scalex":
					image.scaleX = val;
					break;
				case "scaley":
					image.scaleY = val;
					break;
			}
		}
		
		public function getProp(prop:String):Number {
			switch (prop) {
				case "x":
					return image.x;
				case "y":
					return image.y;
				case "scalex":
					return image.scaleX;
				case "scaley":
					return image.scaleY;
				default:
					return 0;
			}
		}
		
		private function mouseDownListener(e:MouseEvent):void {
			offset = new Point(e.localX, e.localY);
			mDown = true;
		}
		
		public function moveBox(e:MouseEvent) {			
			image.x = e.stageX - offset.x;
			image.y = e.stageY - offset.y;
			if (image.x < 100) image.x = 100;
			if (image.y < 100) image.y = 100;
			if (image.x > 1024 - image.width) image.x = 1024 - image.width
			if (image.y > 764 - image.height) image.y = 768 - image.height
		}
		
		private function mouseMoveListener(e:MouseEvent) {
			if(mDown) {
				moveBox(e);
			}
		}
		
		public function toXML(i:int):String {
			var xml:String = "<Image\n";
			xml += "\tindex=\"" + i + "\"\n";
			xml += "\tx=\"" + image.x + "\"\n";
			xml += "\ty=\"" + image.y + "\"\n";
			xml += "\tscalex=\"" + image.scaleX + "\"\n";
			xml += "\tscaley=\"" + image.scaleY + "\"\n";
			xml += ">\n";
			xml += filename;
			xml += "\n</Image>\n";
			return xml;
		}
		
		public function fromXML(xml:String) {
			var pieces:Array = xml.match(">.+</Image>");
			filename = String(pieces[0]).slice(1,-8);
			
			loader.loadSpecific(filename);
			
			pieces = xml.match("x=\".+\"");
			var attr = String(pieces[0]).split('"');
			image.x = attr[3]; image.y = attr[5];
			image.scaleX = attr[7]; image.scaleY = attr[9];
		}
		
		public function remove() {
			if (editor.contains(image)) editor.removeChild(image);
		}
	}
}